package com.zsr.web;

import com.zsr.pojo.Book;
import com.zsr.pojo.Page;
import com.zsr.service.BookService;
import com.zsr.service.impl.BookServiceImpl;
import com.zsr.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前台传回最后一页的页码,因为page的实体类做了验证，所以是永远是最后一页
        int PageNo =WebUtils.parseInt(req.getParameter("pageNo"),1);
        PageNo+=1;
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        bookService.addBook(book);

        //当使用请求转发时，用户提交完请求，浏览器会记录下最后一次请求的全部信息。
        // 当用户按下功能键F5,就会发起浏览器的最后一次请求【表单重复提交】
        //req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);


        //使用重定向 【重定向要加上工程名】
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+PageNo);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取请求的参数==封装成为 Book 对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        // 2、调用 BookService.updateBook( book );修改图书
        bookService.updateBook(book);
        // 3、重定向回图书列表管理页面
        // 地址：/工程名/manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        bookService.deleteBookById(id);
        //使用重定向 【重定向要加上工程名】
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        req.setAttribute("books", books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        Book book = bookService.queryBookById(id);
        req.setAttribute("editBook", book);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req, resp);


    }

    /**
     * 获得page对象，转发到图书列表页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数pageNo和pageSize
        int pageNo =WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize =WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page(pageNo,pageSize):page对象
        Page<Book> page =bookService.page(pageNo,pageSize);
        page.setUrl("manager/bookServlet?action=page");

        //3.保存Page对象到request域中
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}
